<?php

namespace App\Admin\Controllers;

use App\Models\BusinessTime;
use App\Models\SetMeal;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BusinessTimeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '时间段';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BusinessTime());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });

        $grid->column('start', __('开始时间'))->display(function ($time){
            if ($time>=86400-8*3600){
                return '次日'.date('H:i:s',$time);
            }
            return date('H:i:s',$time);
        });

        $grid->column('end', __('结束时间'))->display(function ($time){
            if ($time>=86400-8*3600){
                return '次日'.date('H:i:s',$time);
            }
            return date('H:i:s',$time);
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->where(function ($query) {
                $query->where('start', '>=', strtotime("1970-01-01 ".$this->input));
            },'开始时间')->time();
            $filter->where(function ($query) {
                $query->where('end', '<=', strtotime("1970-01-01 ".$this->input));
            },'结束时间')->time();

        });
//        $grid->actions(function ($actions) {
//            // 获取当前行主键值
//            $id = $actions->getKey();
//            $setmeal = SetMeal::where('business_time_id',$id)->get()->pluck('name', 'id');
////            Scale::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id')
//            if ($setmeal){
//                // 去掉删除
//                $actions->disableDelete();
//            }
//        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BusinessTime::findOrFail($id));

        $show->field('start', __('开始时间'))->as(function ($time){

            return date('H:i:s',$time);
        });
        $show->field('end', __('结束时间'))->as(function ($time){

            return date('H:i:s',$time);
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BusinessTime());

        $form->hidden('id');
        $form->hidden('store_id', __('门店id'));
        $form->time('start', __('开始时间'))->required();
        $form->time('end', __('结束时间'))->required();

        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
            if ($form->end <$form->start){
                $form->end = strtotime('1970-01-02 '.$form->end);  //晚上七点到次日凌晨5点
            }else{
                $form->end = strtotime('1970-01-01 '.$form->end);
            }

            $form->start = strtotime('1970-01-01 '.$form->start);

        });

        return $form;
    }
}
